package com.mall.bargain.service.impl;

import com.mall.bargain.mapper.ReceiveAddressMapper;
import com.mall.bargain.service.IReceiveAddressService;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.ErrorMsg;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbCity;
import com.mall.entity.bargain.BgReceiveAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
 * created with IntelliJ IDEA
 *
 * @author: S
 * @date: 2021/07/19/14:35
 * Other:  发货地址砍价列表业务处理
 */
@Service
public class ReceiveAddressServiceImpl implements IReceiveAddressService {

    @Autowired
    private ReceiveAddressMapper receiveAddressMapper;

    /**
     * 添加收货地址
     *
     * @param receiveAddress 收货信息
     * @return {@link CommonResponse}
     */
    @Override
    public CommonResponse addAddress(BgReceiveAddress receiveAddress) {
        //需要进行收货人，手机号和详细地址的验证
        boolean flag = true;
        String returnMsg = "";
        String pattern = "[\\u4E00-\\u9FA5]{2,10}";
        if (!Pattern.matches(pattern,receiveAddress.getUsername())){
            flag = false;
            returnMsg = ErrorMsg.ERROR_USERNAME_LENGTH_404;
        }
        String tel = "1[0-9]{10}";
        if (!Pattern.matches(tel,receiveAddress.getTelphone())){
            flag = false;
            returnMsg = ErrorMsg.ERROR_TELPHONE_PATTERN_404;
        }
        String addressPattern = "[(a-zA-Z0-9\\u4e00-\\u9fa5)]{1,80}";
        if (!Pattern.matches(addressPattern,receiveAddress.getAddress())){
            flag = false;
            returnMsg = ErrorMsg.ERROR_ADDRESS_LENGTH_404;
        }
        if (!flag){
            return new CommonResponse(CommonStatus.UNVALID.getCode(), returnMsg);
        }else{
            receiveAddressMapper.addAddress(receiveAddress);
            return new CommonResponse(CommonStatus.VALID.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }
    }

    /**
     * 三级城市联动
     * 三个下拉框的情况
     * @param id
     * @return {@link List<TbCity>}
     */
    @Override
    public List<TbCity> findCity(Integer id) {
        return receiveAddressMapper.findCity(id);
    }

    /**
     * 三级城市联动
     * 一个下拉框的情况
     * @return {@link List<TbCity>}
     */
    @Override
    public List<TbCity> findAllCity() {
        //查找出所有省市
        List<TbCity> cities = receiveAddressMapper.findAllCity();
        //2.组装成父子的树形结构
            //找到所有的省份
        List<TbCity> cityList = cities.stream().filter(city ->
                city.getPid() == 0
        ).map((menu) -> {
            /*通过父城市找到每一个父城市对应的下级城市并放入城市实体下的集合中*/
            menu.setChildren(getChildrens(menu, cities));
            return menu;
        }).collect(Collectors.toList());
        return cityList;
    }

    /**
     * 根据用户id查询该用户收货地址
     * @return {@link Integer uid}
     */
    @Override
    public List<BgReceiveAddress> findAddressByUid(Integer uid) {
        return receiveAddressMapper.findAddressByUid(uid);
    }

    /**
     * 查看订单的地址
     * @param id 订单ID
     * @return
     */
    @Override
    public BgReceiveAddress orderAddress(Integer id) {
        return receiveAddressMapper.orderAddress(id);
    }

    /**
     * 过去到每个省市的下级城市
     * @return {@link List<TbCity>}
     */
    private List<TbCity> getChildrens(TbCity root, List<TbCity> cities) {
        List<TbCity> children = cities.stream().filter(city -> {
            return city.getPid().equals(root.getCid());
        }).map(city -> {
            //找到下级城市
            city.setChildren(getChildrens(city, cities));
            return city;
        }).collect(Collectors.toList());
        return children;
    }
}
